require_relative './podfile_helpers.rb'
require_relative '../../../react-native-lab/react-native/packages/react-native/scripts/react_native_pods'
require File.join(File.dirname(`node --print "require.resolve('expo/package.json')"`), "scripts/autolinking")

ENV['RCT_NEW_ARCH_ENABLED'] = '1'

install! 'cocoapods',
         :generate_multiple_pod_projects => true,
         :incremental_installation => true
platform :ios, '15.1'

inhibit_all_warnings!

# Disable expo-updates auto create updates resources in podspec script_phase
$expo_updates_create_updates_resources = false

prepare_react_native_project!

target 'Expo Go' do
  pod 'CocoaLumberjack', '~> 3.5.3'
  pod 'MBProgressHUD', '~> 1.2.0'

  # Required by react-native-maps
  # See https://github.com/react-native-maps/react-native-maps/blob/master/docs/installation.md
  pod 'react-native-google-maps', :path => '../../../node_modules/react-native-maps'

  # Required by firebase core versions 9.x / 10.x (included with SDK 47)
  # See https://github.com/invertase/react-native-firebase/issues/6332#issuecomment-1189734581
  pod 'FirebaseCore', :modular_headers => true
  pod 'FirebaseCrashlytics', :modular_headers => true
  pod 'FirebaseAnalytics', :modular_headers => true
  pod 'GoogleUtilities', :modular_headers => true

  pod 'FirebaseInstallations', :modular_headers => true
  pod 'GoogleDataTransport', :modular_headers => true
  pod 'nanopb', :modular_headers => true
  pod 'FirebaseCoreExtension', :modular_headers => true

  # Expo modules
  use_expo_modules!({
    exclude: [
      'expo-module-template',
      'expo-module-template-local',
      'expo-dev-launcher',
      'expo-dev-client',
      'expo-dev-menu',
      'expo-maps',
      'expo-network-addons',
      'expo-insights',
      'expo-splash-screen',
      'expo-blob',
      '@expo/ui',
      '@expo/app-integrity'
    ],
    includeTests: true,
    flags: {
      :inhibit_warnings => false
    }
  })

  # Install vendored pods.
  use_pods! 'vendored/unversioned/**/*.podspec.json'

  pod 'expo-dev-menu', :path => '../../../packages/expo-dev-menu', :configurations => ['Debug', 'Release']

  config_command = [
    'node',
    '--no-warnings',
    '--eval',
    'require(\'expo/bin/autolinking\')',
    'expo-modules-autolinking',
    'react-native-config',
    '--json',
    '--platform',
    'ios'
  ]
  config = use_native_modules!(config_command)

  react_native_path = '../../../react-native-lab/react-native/packages/react-native'

  use_react_native!(
    :path => react_native_path,
    :hermes_enabled => true,
  )

  post_install do |installer|
    # `installer.pods_project` might be nil for `incremental_installation: true` and no new project generated
    if installer.pods_project
      react_native_post_install(
        installer,
        react_native_path,
        :mac_catalyst_enabled => false
      )
    end

    # Disabled as of CocoaPods 1.8.0.beta1 since pods_project seems to be nil
    # installer.pods_project.main_group.tab_width = '2';
    # installer.pods_project.main_group.indent_width = '2';

    installer.target_installation_results.pod_target_installation_results
      .each do |pod_name, target_installation_result|

      # Enable hermes debugger even on release builds
      if ['React-hermes', 'hermes-engine', 'React-RuntimeHermes'].include?(pod_name)
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << "HERMES_ENABLE_DEBUGGER=1"
        end
      end
      if ['React-jsinspector', 'React-RCTNetwork'].include?(pod_name)
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << "REACT_NATIVE_DEBUGGER_ENABLED=1"
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << "REACT_NATIVE_DEBUGGER_ENABLED_DEVONLY=1"
        end
      end

      # Set `REACT_NATIVE_PATH` to react-native submodule
      target_installation_result.native_target.build_configurations.each do |config|
        config.build_settings["REACT_NATIVE_PATH"] = File.join("${PODS_ROOT}", "..", react_native_path)
      end

      target_installation_result.native_target.build_configurations.each do |config|
        config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.1'
      end

      # On iOS, the Stripe dependency StripePaymentsUI can't seem to find a header that should be there.
      if pod_name == 'StripePaymentsUI'
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['HEADER_SEARCH_PATHS'] ||= ['$(inherited)']
          config.build_settings['HEADER_SEARCH_PATHS'] << '"$(PODS_ROOT)/Headers/Public/StripePayments"'
        end
      end

      if pod_name.end_with?('EXUpdates')
        target_installation_result.native_target.build_configurations.each do |config|
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
          config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'SUPPRESS_EXPO_UPDATES_SERVICE=1'
        end
      end

      # Build React Native with RCT_DEV enabled and RCT_ENABLE_INSPECTOR and
      # RCT_ENABLE_PACKAGER_CONNECTION disabled
      next unless pod_name.start_with?('React')
      target_installation_result.native_target.build_configurations.each do |config|
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)']
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_DEV=1'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_ENABLE_INSPECTOR=0'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_REMOTE_PROFILE=0'
        config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << 'RCT_DEV_SETTINGS_ENABLE_PACKAGER_CONNECTION=0'
      end
    end
  end

  # Test targets
  target 'ExponentIntegrationTests' do
    inherit! :search_paths
  end

  target 'Tests' do
    # `ExpoModulesTestCore` has implicit dependency to `React-Core` which has a resource bundle.
    # To prevent CocoaPods generating new `React-Core` resource bundle and the strange `React-Core-60309c9c` target,
    # this test target should inherit all properties from parents.
    inherit! :complete

    pod 'ExpoModulesTestCore', :path => "../../../packages/expo-modules-test-core/ios"
  end
end
